import { 
  Controller,
  Get,
  Post, Body, Headers, HttpCode,
} from "@nestjs/common";
// 访问路由后返会的数据
import { WealthService } from './wealth.service'
import { WealthService_jyData } from './wealth_jyData.service'
// 引入TypeScript定义, 使用 reference 映入文件
/// <reference path="./wealth_types.ts" />
import * as dayjs from 'dayjs'
import * as colors from 'colors'
import { isdate, isDateString } from '../utils/date'



/* 请求响应 */
@Controller('wealth')
export class WealthController {
  constructor(
    private readonly WealthService: WealthService,
    private readonly WealthService_jyData: WealthService_jyData,
  ) {}

  @Get('/')
  init(): any {
    return this.WealthService.getInit()
  }
  
  // 根据条件筛选
  @Get('/where')
  GetWhere(): any {
    // console.log(111222, this.WealthService.getWhere() )
    return this.WealthService.getWhere()
  }

  /**
   * post 添加数据
   * @param testBody post中数据放置在Body中，使用@Body()来获取
   * @param testHeaders 请求头数据
   * @returns 
   */
  @Post('/postWhere')
  // 默认情况下 post 的请求状态码是 201 
  // 使用 @HttpCode() 装饰器 修改
  @HttpCode(200)
  async postWhere(@Body() testBody, @Headers() testHeaders) {
    const queryBody = Object.keys(testBody).toString()
    // console.log("testBody", testBody, )
    // console.log('queryBody', queryBody)
    // console.log('testHeaders', testHeaders)
    const msg = await this.WealthService_jyData.setMore_wealth_jyData( [testBody] )
    
    if( !queryBody ) return { code: -1, msg: '请求失败', data: [] }
    return {
      code: 0,
      msg: msg,
      data: [],
    }
  }

  @Post('/getSummary')
  @HttpCode(200)
  async getSummary(@Body() summaryBody: WealthTypes.PWC, @Headers() summaryHeaders) {
    const {startTime, endTime, codeName} = summaryBody
    const beforeTime = dayjs().add(-1, "day").format("YYYY-MM-DD"),
      nowTime = dayjs().format("YYYY-MM-DD");
    
    const params: WealthTypes.PWC = {
      startTime: startTime ? isDateString(startTime) ? startTime : beforeTime : beforeTime,
      endTime: startTime ? isDateString(endTime) ? endTime : nowTime : nowTime
    }
    if (summaryBody.codeName) params['codeName'] = summaryBody.codeName

    // const data = await this.WealthService.postWealthCondition(params)
    const data = await this.WealthService.getSummeryData({sTime: startTime, eTime: endTime})

    return {
      code: 0,
      msg: '数据获取成功！！！', 
      data: data
    }
  }

  @Post('/selectData')
  @HttpCode(200)
  async selectData(@Body() summaryBody: WealthTypes.PWC, @Headers() summaryHeaders) {
    // console.log('@Body', summaryBody, "@Headers", summaryHeaders)
    const {startTime, endTime, codeName} = summaryBody
    const beforeTime = dayjs().add(-1, "day").format("YYYY-MM-DD"),
      nowTime = dayjs().format("YYYY-MM-DD");
    
    const params: WealthTypes.PWC = {
      startTime: startTime ? isdate(startTime) ? startTime : beforeTime : beforeTime,
      endTime: startTime ? isdate(endTime) ? endTime : nowTime : nowTime
    }
    if (summaryBody.codeName) params['codeName'] = summaryBody.codeName

    const data = await this.WealthService.postWealthCondition(params)
    // console.log('data /wealth/getSummary', data)
    return {
      code: 0,
      msg: '数据获取成功！！！', 
      data: data
    }
  }
}

